package org.groupg.practice.swingx.demo;
import com.formdev.flatlaf.FlatIntelliJLaf;
import org.jdesktop.swingx.JXFrame;

import javax.swing.*;
import java.awt.*;
import java.util.Date;

public class MainApplication {
    private static int nextId = 1;

    public static void main(String[] args) throws UnsupportedLookAndFeelException {
        UIManager.setLookAndFeel(new FlatIntelliJLaf());
        JXFrame frame = new JXFrame("SwingX 增删改查示例");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(800, 600);
        frame.setLayout(new BorderLayout(10, 10));

        // 创建表格
        PersonTable personTable = new PersonTable();
        frame.add(personTable, BorderLayout.CENTER);

        // 添加初始数据
        personTable.addPerson(nextId++, "张三", new Date(), "男");
        personTable.addPerson(nextId++, "李四", new Date(), "女");
        personTable.addPerson(nextId++, "王五", new Date(), "其他");

        // 表格事件处理
        personTable.setTableActionListener(new PersonTable.TableActionListener() {
            @Override
            public void onAddRequest() {
                // 创建表单对话框
                PersonForm form = new PersonForm(frame);
                form.clearForm();
                form.setEditable(true);

                // 设置表单保存监听器
                form.setFormActionListener((id, name, birthDate, gender) -> {
                    personTable.addPerson(nextId++, name, birthDate, gender);
                });

                form.setVisible(true);
            }

            @Override
            public void onEditRequest(int selectedRow) {
                // 获取选中行数据
                Object[] data = personTable.getPersonData(selectedRow);
                int id = (int) data[0];

                // 创建表单对话框
                PersonForm form = new PersonForm(frame);
                form.loadPerson(
                    id,
                    (String) data[1],
                    (Date) data[2],
                    (String) data[3]
                );
                form.setEditable(true);

                // 设置表单保存监听器
                form.setFormActionListener((formId, name, birthDate, gender) -> {
                    // 更新表格数据
                    for (int i = 0; i < personTable.getTableModel().getRowCount(); i++) {
                        if ((int) personTable.getTableModel().getValueAt(i, 0) == formId) {
                            personTable.updatePerson(i, name, birthDate, gender);
                            break;
                        }
                    }
                });

                form.setVisible(true);
            }

            @Override
            public void onDeleteRequest(int selectedRow) {
                int confirm = JOptionPane.showConfirmDialog(
                    frame,
                    "确定要删除这条记录吗?",
                    "确认删除",
                    JOptionPane.YES_NO_OPTION
                );

                if (confirm == JOptionPane.YES_OPTION) {
                    personTable.deletePerson(selectedRow);
                }
            }
        });

        frame.setVisible(true);
    }
}